import cv2
import numpy as np
if __name__ == '__main__':
    path = 'work1.jpg'
    image_np = cv2.imread(path)
    image_np_hsv = cv2.cvtColor(image_np, cv2.COLOR_BGR2HSV)
    white_low = np.array([0, 0, 140])
    white_high = np.array([170, 20, 235])
    mask = cv2.inRange(image_np_hsv, white_low, white_high)



    color_image_np = cv2.bitwise_and(image_np, image_np, mask=mask)

    color_image_gray = cv2.cvtColor(color_image_np, cv2.COLOR_BGR2GRAY)
    thresh = 122
    maxal = 255
    ret, color_image_gray = cv2.threshold(color_image_gray, thresh, maxal, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)

    kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
    # kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
    # cv2.imshow('fist_image', color_image_gray)
    morph_image1 = cv2.erode(color_image_gray, kernel1)
    # cv2.imshow('fist_image', morph_image1)
    # morph_image2 = cv2.dilate(morph_image1, kernel2)
    # cv2.imshow('second_image', morph_image2)

    cv2.imshow('', morph_image1)
    cv2.imwrite('zuoye1.jpg', morph_image1)
    cv2.waitKey(0)